import {wxRequest} from '../../utils/server.js';
import store_share from "../../component/painter/palette/store";
const storeShare = new store_share()
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'), 
    adList: [], 
    id: '',
    swiperList: [],  
    show: false,
    mdc_show: false,
    playIndex:''
  },

  onLoad: function (option) {
    this.setData({
      id: option.id,
      sharedCusId: option.sharedCusId || '',
      empUserId: option.empUserId || ''
    })
  },

  onShow: function () {
    this.getDetail(); 
    if (wx.getStorageSync('Authorization')) {
      this.getAttend();//查询关注
    } 
  },


  //获取产品详情
  getDetail: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/org/store/detail?id=' + this.data.id,
      method: 'POST'
    }, (res) => {
      var detail = res.vo;
      if (detail.offInfoJson) {
        detail.offInfoJson = JSON.parse(detail.offInfoJson)
      }
      this.setData({
        detail: detail,
        adList: res.vo.coverImgs
      })
    })

  },
 


  //预览户型图
  imageList: function (e) {
    let current = this.data.uploadUrl + e.target.dataset.src;
    let urls = e.target.dataset.urls;
    if (urls && urls.length) {
      for (var i = 0; i < urls.length; i++) {
        urls[i] = this.data.uploadUrl + urls[i].imgPath
      };
    }
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    });
  },


  //分享 
  onShareAppMessage: function () {
    var image = this.data.uploadUrl + this.data.detail.coverImgs[0].imgPath;
    var uId = wx.getStorageSync('userInfo').id || ''
    return {
      title: this.data.detail.goodsName,
      imageUrl: image,
      path: '/pages/good/detail/index?id=' + this.data.id + '&sharedCusId=' + uId
    }  
  },



  //获取二维吗
  getCode() {
    var that = this
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + '/odr/mall/getWxaCodeUnlimit',
      contentType: "application/x-www-form-urlencoded",
      method: 'POST',
      data: {
        page: '/pages/store/detail',
        width: 750,
        scene: 'id=' + this.data.id
      }
    }, (res) => { 
      that.setData({
        qrCode: res.url
      }, () => {
        that.toShare()
      })
    })
  },


  //分享
  toShare() {
    var qrCode = this.data.qrCode
    this.setData({
      shareImgTemplate: storeShare.palette(qrCode, this.data.detail, 0)
    });
    wx.showLoading({
      title: "努力生成中..."
    });
  },

  //图片生成完成后呈现在页面上
  onShareImgOK(e) {
    this.setData({
      shareImgUrl: e.detail.path
    });
    wx.hideLoading();
    this.previewShareImg();
  },
  previewShareImg(e) {
    let url = this.data.shareImgUrl;
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },

  toArticle: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id,'id')
    wx.navigateTo({
      url: '/pages/activity/detail?id=' + id,
    })
    
  },


  //添加关注 
  customerAttend: function () {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/addUserAttend?stoId=' + this.data.id,
      method: 'post'
    }, (res) => {
      that.setData({
        arrt: true
      })

      wx.showToast({
        icon: 'none',
        title: '收藏成功'
      })
    })
  },

  //取消关注
  delCustomerAttend: function () {
    var that = this;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/deleteUserAttend',
      method: 'post',
      contentType: 'application/x-www-form-urlencoded',
      data: {
        stoId: this.data.id,
        stoIdList: [this.data.id]
      }
    }, (res) => {
      that.setData({
        arrt: false
      })
      wx.showToast({
        icon: 'none',
        title: '取消收藏'
      })
    })
  },


  // 查询关注
  getAttend: function () {
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/org/protected/storeAttend/existsUserAttend?stoId=' + this.data.id,
      method: 'POST'
    }, (res) => {
      this.setData({
        arrt: res.flag
      })
    })
  },

//打电话
toTel:function(e){
  var mobile = e.currentTarget.dataset.mobile;
  wx.makePhoneCall({
    phoneNumber: mobile,
  })
},


//地图导航
  goMap:function(){
    var that=this; 
    wx.getLocation({ 
      type: 'wgs84',  
      success: function (res) {
        wx.openLocation({ 
          latitude: Number(that.data.detail.lat),//要去的纬度-地址
          longitude: Number(that.data.detail.lng),//要去的经度-地址
          name: that.data.detail.detailAddress,
          address: that.data.detail.detailAddress
        })
      }
    })
  },


  // 封面点击的时候相应的操作  获取视频在JS里的参数属性 
  //重点：点击图片接下来视频进行播放，但是视频播放后直接触发了视频的pause() 结局的方案是，延时150ms后再进行视频的播放，就完美解决了
  bindPlay: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.index 
    // 没有播放时播放视频
    if (!this.data.playIndex) { 
      this.setData({
        playIndex: id
      })
      var videoContext = wx.createVideoContext(id) //这里对应的视频id
      videoContext.play()
    } else { // 有播放时先将prev暂停，再播放当前点击的current 
      var videoContextPrev = wx.createVideoContext(this.data.playIndex)
      if (this.data.playIndex != id) { 
        videoContextPrev.pause()
      }
      this.setData({
        playIndex: id
      })
      var videoContextCurrent = wx.createVideoContext(id)
      videoContextCurrent.play()
    }
 

    that.setData({
      mdc_show: true
    })

  },


  outPlay: function () {
    this.setData({
      mdc_show: false
    })
  },



  //安卓系统能检测到 video touchemove 滑动的x距离，已此作为切换的swiper的凭证
  //ios系统，检测不到video touchemove 滑动的x距离，通过cover-view 点击事件进行切换
  mdc_move1: function (e) {
    var that = this;  
    
    var videoContextPrev = wx.createVideoContext('mdcVideo1');
    videoContextPrev.pause()

    if (e.touches[0].clientX > 20) {
      that.setData({
        mdc_show: false,
      })
    }
  },
  returnquanping: function (e) {
    var that = this;
    that.setData({
      mdc_show: false,
    })
  },

})